package daos;
import com.j256.ormlite.dao.*;
import com.j256.ormlite.stmt.*;
import com.j256.ormlite.support.*;
import entities.*;
import java.util.*;
import java.util.logging.*;
import java.util.logging.Logger;

/**
 * This Class is used to handle 
 * the DAO methods of the Function class 
 * @author Juanan JS  juanantonios@gmail.com
 */
public class FunctionDao
{
    private ConnectionSource objConnectionSource=null;
    /**
     * Constructor:
     * @param objConnectionSource_ 
     * An active instance of a ConnectionSource
     */
    public FunctionDao(ConnectionSource objConnectionSource_)
    {
        this.objConnectionSource=objConnectionSource_;
    }
    
    /**
     * @param objuser object instance of type UsrUser
     * @return A List of Functions the given user has Access
     */    
    public List<FunFunction> findByuser(UsrUser objuser)
    {
        List<FunFunction> objFunctionList=null;
        try
        {
            //JS: do the function dao
            Dao<FunFunction, List> objFunctionDao = 
                    DaoManager.createDao(objConnectionSource, FunFunction.class);
            //JS: do the query for user login
            QueryBuilder<FunFunction, List> queryBuilder =
                    objFunctionDao.queryBuilder();
            //JS: all access list for this group
            AccessDao objAccessDao=new AccessDao(this.objConnectionSource);
            List<AccAccess> objAccessList=
                    objAccessDao.findByGroupId(objuser.getGrpId().getGrpId());
            //Make the query
            //JS: do the query for user login
            queryBuilder.where().in(AccAccess.ACC_ID, objAccessList);
            PreparedQuery<FunFunction> preparedQuery = queryBuilder.prepare();
            //JS: query for all accounts that have that login
            objFunctionList = objFunctionDao.query(preparedQuery);
        } catch (Exception ex)
        {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return objFunctionList;
    }
}
